﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
namespace TestDxfDocument
{
    class Rectangular_tool_handle_with_indexable_blade
    {
        public static void Rectangular_tool_handle(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-----------------------------------------------------------------------------------------------------
            double scale = 1; //放大系数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            //double type = Convert.ToDouble(data["type"])/*"L型菱形"*/;
            double type = 1;

            if (type == 1)//K型刀杆
            {
                //定义参数-------------------------------------------------------------------------------------
                double h1 = 8;
                double h2 = 8;
                double b = 8;
                double l1 = 60;
                double d = 6.35;
                double l2 = 25;
                double f = 10;
                double jd1 = 75;
                double jd2 = 90;
                double jd3 = 7;
                double jd4 = 7;

                //点--------------------------------------------------------------------------------------------
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = fun.Polar(v1, (180 - jd1) * pi / 180, d);
                Vector2 v3 = fun.Polar(v2, (180 - ((180 - ((360 - jd2 * 2) / 2)) + jd1)) * pi / 180, d);
                Vector2 v4 = fun.Inters(v3, fun.Polar(v3, -jd1 * pi / 180, l1), v1, fun.Polar(v1, (180 - ((180 - ((360 - jd2 * 2) / 2)) + jd1)) * pi / 180, l1));
                Vector2 v5 = fun.Inters(v2, fun.Polar(v2, 90 * pi / 180, l1), new Vector2(v1.X + l1, v1.Y + f), new Vector2(v1.X - l1, v1.Y + f));
                Vector2 v6 = new Vector2(v2.X + l2, v1.Y + f);
                Vector2 v7 = new Vector2(v6.X, v6.Y - b);
                Vector2 v8 = new Vector2(v7.X + (l1 - l2 - d * Math.Cos(jd1 * pi / 180)), v7.Y);
                Vector2 v9 = new Vector2(v8.X, v8.Y + b);
                Vector2 v11 = new Vector2(v5.X, v5.Y + 2 * f);
                Vector2 v10 = fun.Inters(v11, fun.Polar(v11, -(90 - jd3) * pi / 180, l1), new Vector2(v11.X, v11.Y - h1), new Vector2(v11.X + l1, v11.Y - h1));
                Vector2 v12 = fun.Inters(v11, fun.Polar(v11, jd4 * pi / 180, l1), new Vector2(v11.X + l2, v10.Y), new Vector2(v11.X + l2, v10.Y + l1));
                Vector2 v13 = new Vector2(v11.X + l2, v10.Y);
                Vector2 v14 = new Vector2(v8.X, v13.Y);
                Vector2 v15 = new Vector2(v14.X, v14.Y + h2);
                Vector2 v16 = new Vector2(v13.X, v15.Y);
                Vector2 v17 = fun.Polar(v11, jd4 * pi / 180, fun.CalDistance(v11, v12) / 10);
                Vector2 v18 = fun.Polar(v10, 0 * pi / 180, fun.CalDistance(v11, v12) / 10);
                Vector2 v19 = fun.Polar(v11, -(90 - jd3) * pi / 180, fun.CalDistance(v11, v12) / 10);
                Vector2 v20 = fun.Polar(v19, jd4 * pi / 180, d);
                Vector2 v21 = fun.Polar(v11, jd4 * pi / 180, d);
                Vector2 v22 = fun.Inters(v2, v4, v1, v3);

                //作图部分-------------------------------------------------------------------------------------------------
                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v1));
                polyline1.Vertexes.Add(new LwPolylineVertex(v2));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v1));
                fun.PolyFill(dxf, polyline1, 0.3, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v2));
                polyline2.Vertexes.Add(new LwPolylineVertex(v5));
                polyline2.Vertexes.Add(new LwPolylineVertex(v6));
                polyline2.Vertexes.Add(new LwPolylineVertex(v7));
                polyline2.Vertexes.Add(new LwPolylineVertex(v4));
                fun.PolyFill(dxf, polyline2, 0.3, contour);

                LwPolyline polyline3 = new LwPolyline();
                polyline3.Vertexes.Add(new LwPolylineVertex(v6));
                polyline3.Vertexes.Add(new LwPolylineVertex(v9));
                polyline3.Vertexes.Add(new LwPolylineVertex(v8));
                polyline3.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, polyline3, 0.3, contour);

                LwPolyline polyline4 = new LwPolyline();
                polyline4.Vertexes.Add(new LwPolylineVertex(v10));
                polyline4.Vertexes.Add(new LwPolylineVertex(v11));
                polyline4.Vertexes.Add(new LwPolylineVertex(v12));
                polyline4.Vertexes.Add(new LwPolylineVertex(v13));
                polyline4.Vertexes.Add(new LwPolylineVertex(v10));
                fun.PolyFill(dxf, polyline4, 0.3, contour);

                LwPolyline polyline5 = new LwPolyline();
                polyline5.Vertexes.Add(new LwPolylineVertex(v13));
                polyline5.Vertexes.Add(new LwPolylineVertex(v14));
                polyline5.Vertexes.Add(new LwPolylineVertex(v15));
                polyline5.Vertexes.Add(new LwPolylineVertex(v16));
                fun.PolyFill(dxf, polyline5, 0.3, contour);

                LwPolyline polyline6 = new LwPolyline();
                polyline6.Vertexes.Add(new LwPolylineVertex(v19));
                polyline6.Vertexes.Add(new LwPolylineVertex(v20));
                polyline6.Vertexes.Add(new LwPolylineVertex(v21));
                fun.PolyFill(dxf, polyline6, 0.3, contour);

                LwPolyline polyline7 = new LwPolyline();
                polyline7.Vertexes.Add(new LwPolylineVertex(v17));
                polyline7.Vertexes.Add(new LwPolylineVertex(v18));
                fun.PolyFill(dxf, polyline7, 0.3, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //上侧
                fun.dimrot1(dxf, v11, v12, 0, h1, 1, height, arrowsize, exline, "l2=" + Math.Round(l2, 1), "", "");
                //左侧
                fun.dimrot1(dxf, v1, v5, 90, l2 / 2, 1, height, arrowsize, exline, "f=" + Math.Round(f, 1), "", "");
                fun.dimrot1(dxf, v11, v10, 90, l2 / 2, 1, height, arrowsize, exline, "h1=" + Math.Round(h1, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v1, v8, 180, f, 1, height, arrowsize, exline, "l1=" + Math.Round(l1, 1), "", "");
                //右侧
                fun.dimrot1(dxf, v8, v9, -90, l2 / 2, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");
                fun.dimrot1(dxf, v15, v14, -90, l2 / 2, 1, height, arrowsize, exline, "h2=" + Math.Round(h2, 1), "", "");
            }









            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
